<?php

require_once './application/core/CoreBase.php';
require_once './application/models/Objets/Dashboard.php';
require_once './application/models/Tables/UtilisateurDashboard.php';

class DashboardCore extends CoreBase
{
	public function ajouterModuleUtilisateur($idUser, $idModule) {
		$table = new TableUtilisateurDashboard();
		if (!isset($idUser)) {
			$idUser = 0;
		}
		if ($table->getNbModuleDashboard($idUser, $idModule) == 0) {
			$table->ajouterModuleDashboard($idUser, $idModule);
			return "1";
		} else {
			return "0";
		}
	}
	
	public function retirerModuleUtilisateur($idUser, $idModule) {
		$table = new TableUtilisateurDashboard();
		if (!isset($idUser)) {
			$idUser = 0;
		}
		if ($table->getNbModuleDashboard($idUser, $idModule) == 1) {
			$table->retirerModuleDashboard($idUser, $idModule);
			return "1";
		} else {
			return "0";
		}
	}
	
	public function getListDashBoard($id) {
		if (!isset($id)) {
			$id = 0;
		}
		$lst = ObjectDashboard::listeDashboard($id);
		return $lst;
	}
	
	public function getListDashBoardByUser($idUser) {
		if (!isset($idUser)) {
			$idUser = 0;
		}
		$lst = ObjectDashboard::listeDashboardByUser($idUser);
		$lstObj = array(null,null,null,null);
		$i = 0;
		$k = 0;
		$count = count($lst);
		if ($count > 0) {
			while (isset($lst[$i])) {
				$j = 0;
				
				while (isset($lst[$i]) && $j < 4) {
					$obj = $lst[$i];
					$lstObj[$j][$k] = $obj;
					$i++;
					$j++;
				}
				$k++;
			}
		}
		return array($count, $lstObj);
	}
	
	public function ajouterModule($libelle, $url, $desc, $refresh, $img, $auteur, $email) {
		$table = new TableDashboard();
		if ($table->getNbModule($url) == 0) {
			$table->ajouterModule($libelle, $url, $desc, $refresh, $img, $auteur, $email);
			return true;
		} else {
			return false;
		}
	}
	
	public function installerModule($file, $folder) {
		$ok = true;
		if (! empty ( $file )) {
			$tempFile = $file['userfile']['tmp_name'];
			$fileName = $file['userfile']['name'];
			$targetPath = APPLICATION_PATH . $folder . '/';
			$targetFile =  str_replace('//','/',$targetPath) . $fileName;
			if (move_uploaded_file ( $tempFile, $targetFile )) {
				//DECOMPRESION ET INSTALLATION DU ZIP
				$common = new CommonCore($this);
				if ($content = $common->unzip($targetFile, $targetPath)) {
					//RECUPERATION DU REPERTOIRE
					$dir = $targetPath . substr($fileName, 0, strpos($fileName, "."));
					//LECTURE DU FICHIER DE CONFIGURATION
					$ressource = fopen($dir . "/install.json", 'r');
					if ($ressource) {
						$content = fread($ressource, filesize($dir . "/install.json"));
						$json = Zend_Json::decode($content);
						if (!isset($json["name"])) {
							$ok = false;
						}
						//COPIE ET RENOMMAGE DE L'IMAGE
						if (isset($json["img"]) && $ok) {
							$ext = substr($json["img"], strpos($json["img"], "."));
							if ($ext == ".jpeg" || $ext == ".jpg" ||  $ext == ".png" ||  $ext == ".gif" ) {
								if (!rename($dir . "/" . $json["img"], APPLICATION_PATH . "/public/img/module/" . $json["name"] . $ext)) {
									$ok =  false;
								}
							} else {
								$ok = false;
							}
						}
						//COPIE DE LA VUE
						if (isset($json["view"]) && $ok) {
							if (!rename($dir . "/" . $json["view"], APPLICATION_PATH . "/application/views/scripts/module/" . $json["name"] . ".phtml")) {
								$ok = false;
							}
						}
						//COPIE DE LA CLASSE
						if (isset($json["class"]) && $ok) {
							if (!rename($dir . "/" . $json["class"], APPLICATION_PATH . "/application/core/Module/" . $json["name"] . ".php")) {
								$ok = false;
							}
						}
						if ($ok) {
							if (!$this->ajouterModule($json["libelle"], $json["name"], $json["desc"], 0, $json["name"] . $ext, $json["auteur"], $json["email"])) {
								$ok = false;
							} else {
								$ok = true;
							}
						} else {
							$this->desinstallerModule($json["name"]);
						}
					}
					//SUPPRESSION DES FICHIERS TEMPORAIRES
					if (file_exists($targetPath . $fileName)) {
						@unlink($targetPath . $fileName);
						$common->deleteDir($dir);
					}
				}
			} else {
				$ok =  false;
			}
		} else {
			$ok = false;
		}
		return $ok;
	}
	
	public function desinstallerModule($module, $erase=0) {
		//SUPPRESSION DE L'ENREGISTREMENT EN BASE
		$retour = 0;
		if ($erase != 0) {
			$table = new TableDashboard();
			$retour = $table->retirerModule($module);
			if (!$retour) {
				return false;
			}
		}
		//SUPPRESSION DES FICHIERS
		if (file_exists(APPLICATION_PATH . "/application/views/scripts/module/" . $module . ".phtml")) {
			@unlink(APPLICATION_PATH . "/application/views/scripts/module/" . $module . ".phtml");
		}
		if (file_exists(APPLICATION_PATH . "/application/core/Module/" . $module . ".php")) {
			@unlink(APPLICATION_PATH . "/application/core/Module/" . $module . ".php");
		}
		if (file_exists(APPLICATION_PATH . "/public/img/module/" . $module . ".jpg")) {
			@unlink(APPLICATION_PATH . "/public/img/module/" . $module . ".jpg");
		} else if (file_exists(APPLICATION_PATH . "/public/img/module/" . $module . ".jpeg")) {
			@unlink(APPLICATION_PATH . "/public/img/module/" . $module . ".jpeg");
		} else if (file_exists(APPLICATION_PATH . "/public/img/module/" . $module . ".png")) {
			@unlink(APPLICATION_PATH . "/public/img/module/" . $module . ".png");
		} else if (file_exists(APPLICATION_PATH . "/public/img/module/" . $module . ".gif")) {
			@unlink(APPLICATION_PATH . "/public/img/module/" . $module . ".gif");
		}
		return true;
	}
	
}

?>
